package com.chengcang.admin.modules.busi.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.chengcang.admin.common.annotation.LogOperation;
import com.chengcang.admin.common.utils.ExcelUtils;
import com.chengcang.admin.modules.busi.dto.BusiAddressDTO;
import com.chengcang.admin.modules.busi.service.BusiAddressService;
import com.chengcang.admin.modules.security.user.SecurityUser;
import com.chengcang.admin.modules.sys.excel.BusiAddressExcel;
import com.chengcang.common.constant.Constant;
import com.chengcang.common.exception.CustomException;
import com.chengcang.common.page.PageData;
import com.chengcang.common.utils.Result;
import com.chengcang.common.validator.AssertUtils;
import com.chengcang.common.validator.ValidatorUtils;
import com.chengcang.common.validator.group.AddGroup;
import com.chengcang.common.validator.group.DefaultGroup;
import com.chengcang.common.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;


/**
 * 地区表
 *
 * @author zhanli
 * @since 1.0.0 2023-01-10
 */
@RestController
@RequestMapping("busi/address")
@Api(tags="地址表")
public class BusiAddressController {
    @Autowired
    private BusiAddressService busiAddressService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<BusiAddressDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<BusiAddressDTO> page = busiAddressService.page(params);
        return new Result<PageData<BusiAddressDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<BusiAddressDTO> get(@PathVariable("id") Long id){
        BusiAddressDTO data = busiAddressService.get(id);

        return new Result<BusiAddressDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(@RequestBody BusiAddressDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        dto.setTenantId(SecurityUser.getTenantId());
        dto.setAddrNo(IdWorker.getIdStr());
        busiAddressService.save(dto);
        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(@RequestBody BusiAddressDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        dto.setDefaulted(null);
        dto.setDeleted(null);
        if (!Pattern.matches("^[0-9-]{13}$",dto.getAddressMobile())){
            throw new CustomException("电话格式错误");
        }
        busiAddressService.update(dto);
        return new Result();
    }

    @GetMapping("defaultAddress/{id}")
    @ApiOperation("设置默认地址")
    @LogOperation("设置默认地址")
    public Result defaultAddress(@PathVariable String id){
        //效验数据
        AssertUtils.isBlank(id, "id");
        busiAddressService.setDefaultAddress(id);
        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        busiAddressService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<BusiAddressDTO> list = busiAddressService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, BusiAddressExcel.class);
    }

}